package com.baozun.houji.goods.viewmodel

import androidx.lifecycle.MutableLiveData
import com.baozhun.mall.common.base.BaseApplication
import com.baozhun.mall.common.model.bean.GoodsInfoBean
import com.baozhun.mall.common.network.mCommonApiService
import com.baozhun.mall.common.util.CacheUtil
import com.baozun.houji.goods.R
import com.baozun.houji.goods.network.homeApiService
import com.blankj.utilcode.util.ToastUtils
import me.hgj.jetpackmvvm.base.viewmodel.BaseViewModel
import me.hgj.jetpackmvvm.ext.request

/**
 * @Title：商品详情ViewModel
 * @Author：xuefeng.zhu
 * @Since：2022/11/4
 * @Version：1.0.0
 */
class GoodsDetailViewModel : BaseViewModel() {

    var mGoodsDetailBean = MutableLiveData<GoodsInfoBean>()
    var mRecommendGoodsInfoBean = MutableLiveData<MutableList<GoodsInfoBean>>()
    var mChooseSkuInfo = MutableLiveData(BaseApplication.mInstance.getString(com.baozhun.mall.res.R.string.please_choose_spec))

    /**
     * 获取用户信息
     */
    fun getUserInfo() {
        if (CacheUtil.isLogin())
            request({ mCommonApiService.getUserInfo() }, {
                CacheUtil.setUser(it)
            })
    }

    fun getGoodsInfo(goodsId: String, goodsType: String?) {
        request({ homeApiService.getGoodsInfo(goodsId, goodsType) }, {
            mGoodsDetailBean.value = it
        }, isShowDialog = false)
    }

    fun getRecommendGoods(goodsId: String, goodsType: String?) {
        request({ homeApiService.getRecommendGoodsInfo(goodsId, goodsType) }, {
            mRecommendGoodsInfoBean.value = it.recommend_goods_list
        }, isShowDialog = false)
    }

    /**
     * 添加收藏
     */
    fun addCollect(goodsId: String, goodsType: String?, addSuccessCallBack: () -> Unit) {
        request({ mCommonApiService.addCollect(goodsId, goodsType) }, {
            addSuccessCallBack.invoke()
            ToastUtils.showShort(com.baozhun.mall.res.R.string.collection_success)
        }, isShowDialog = true)
    }

    /**
     * 删除收藏
     */
    fun cancelCollect(goodsId: String, goodsType: String?, deleteSuccessCallback: () -> Unit) {
        request({ mCommonApiService.cancelCollection(goodsId, goodsType) }, {
            deleteSuccessCallback.invoke()
            ToastUtils.showShort(com.baozhun.mall.res.R.string.operation_success)
        }, isShowDialog = true)
    }
}